﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Weibo4Sharp.Model;

namespace Weibo4SharpWeb.Controllers
{
    public class RepostController : WeiboControllerBase
    {
        //
        // GET: /Repost/Detail/{id}

        public ActionResult Detail(long id)
        {
            var s = GetAuthenticatedService();

            var weibo = s.GetWeibo(id);

            if (weibo == null)
            {
                return NewHomeResult();
            }

            s.ListWeiboCountsBindToWeibos(new List<WeiboStatus> { weibo });

            ViewBag.Weibo = weibo;

            var reposts = s.ListReposts(id);

            s.ListWeiboCountsBindToWeibos(reposts);

            ViewBag.Reposts = reposts;

            return View();
        }

        [HttpPost]
        public ActionResult Create(long id, string status)
        {
            if(status.Trim() == string.Empty)
            {
                status = "Repost";
            }

            var s = GetAuthenticatedService();

            var repost = s.Repost(id, status);            

            if (Request.IsAjaxRequest())
            {
                ViewData["isDetail"] = true;
                ViewData["hideRepost"] = true;

                return PartialView("_Weibo", repost);
            }

            return RedirectToAction("Detail", new { id = id });
        }

    }
}
